﻿using System;
using System.Windows.Forms;
using Bioinformatics.Types;

namespace PsomlGui
{
    public partial class PreferencesForm : Form
    {
        public PreferencesForm()
        {
            InitializeComponent();
            PsomlConfiguration = new PsomlConfiguration();
        }

        public string CudaArgurmentsOutputFilePath { get; set; }
        public string CudaExecutableFilePath { get; set; }

        public string EntrezBrowserExecutablePath { get; set; }
        public string EntrezBrowserOutputFilePath { get; set; }
        public PsomlConfiguration PsomlConfiguration { get; set; }

        private void PreferencesForm_Load(object sender, EventArgs e)
        {
            uxEntrezBrowserExecutablePath.Text = EntrezBrowserExecutablePath;
            uxEntrezBrowserOutputFilePath.Text = EntrezBrowserOutputFilePath;
            uxCudaExecutablePath.Text = CudaExecutableFilePath;
            uxCudaArgumentsOutputFilePath.Text = CudaArgurmentsOutputFilePath;
            uxCudaTopologyOutputFilePath.Text = PsomlConfiguration.InputFile;
            uxMaximumGenerations.Value = new decimal(PsomlConfiguration.GenerationMax);
            uxMaximumLengthDelta.Value = new decimal(PsomlConfiguration.LengthDeltaMax);
            uxMinimumLengthDelta.Value = new decimal(PsomlConfiguration.LengthDeltaMin);
            uxMaximumLength.Value = new decimal(PsomlConfiguration.LengthMax);
            uxMinimumLength.Value = new decimal(PsomlConfiguration.LengthMin);
            uxNeighbourhoodSize.Value = new decimal(PsomlConfiguration.NeighborhoodSize);
            uxSwarmSize.Value = new decimal(PsomlConfiguration.SwarmSize);
            uxMinimumRandomVector.Value = new decimal(PsomlConfiguration.RandomVectorMin);
            uxMaximumRandomVector.Value = new decimal(PsomlConfiguration.RandomVectorMax);
            uxLocalCoefficient.Value = new decimal(PsomlConfiguration.LocalCoefficient);
            uxLocalBestCoefficient.Value = new decimal(PsomlConfiguration.LocalBestCoefficient);
            uxGlobalCoefficient.Value = new decimal(PsomlConfiguration.GlobalCoefficient);
            uxNeighbourhoodCoefficient.Value = new decimal(PsomlConfiguration.NeighbourhoodCoefficient);
        }

        private void uxBrowseButton_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog {Filter = "Executable Application *.exe|*.exe"};
            if (openFileDialog.ShowDialog() == DialogResult.OK)
                uxEntrezBrowserExecutablePath.Text = openFileDialog.FileName;
        }

        private void uxBrowseOutputFilePath_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog {Filter = "Genetic Sequence Collection *.gsc|*.gsc"};
            if (openFileDialog.ShowDialog() == DialogResult.OK)
                uxEntrezBrowserOutputFilePath.Text = openFileDialog.FileName;
        }

        private void uxCancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void uxCudaArgumentsOutputFilePathBrowse_Click(object sender, EventArgs e)
        {
            var saveFileDialog = new SaveFileDialog {Filter = "CUDA Arguments File *.caf|*.caf"};
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
                uxCudaArgumentsOutputFilePath.Text = saveFileDialog.FileName;
        }

        private void uxCudaExecutablePathBrowse_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog {Filter = "Executable Application *.exe|*.exe"};
            if (openFileDialog.ShowDialog() == DialogResult.OK)
                uxCudaExecutablePath.Text = openFileDialog.FileName;
        }

        private void uxSaveButton_Click(object sender, EventArgs e)
        {
            EntrezBrowserExecutablePath = uxEntrezBrowserExecutablePath.Text;
            EntrezBrowserOutputFilePath = uxEntrezBrowserOutputFilePath.Text;
            CudaExecutableFilePath = uxCudaExecutablePath.Text;
            CudaArgurmentsOutputFilePath = uxCudaArgumentsOutputFilePath.Text;
            PsomlConfiguration.GenerationMax = decimal.ToInt32(uxMaximumGenerations.Value);
            PsomlConfiguration.LengthDeltaMax = decimal.ToSingle(uxMaximumLengthDelta.Value);
            PsomlConfiguration.LengthDeltaMin = decimal.ToSingle(uxMinimumLengthDelta.Value);
            PsomlConfiguration.LengthMax = decimal.ToSingle(uxMaximumLength.Value);
            PsomlConfiguration.LengthMin = decimal.ToSingle(uxMinimumLength.Value);
            PsomlConfiguration.NeighborhoodSize = decimal.ToInt32(uxNeighbourhoodSize.Value);
            PsomlConfiguration.SwarmSize = decimal.ToInt32(uxSwarmSize.Value);
            PsomlConfiguration.RandomVectorMin = decimal.ToSingle(uxMinimumRandomVector.Value);
            PsomlConfiguration.RandomVectorMax = decimal.ToSingle(uxMaximumRandomVector.Value);
            PsomlConfiguration.LocalBestCoefficient = decimal.ToSingle(uxLocalBestCoefficient.Value);
            PsomlConfiguration.GlobalCoefficient = decimal.ToSingle(uxGlobalCoefficient.Value);
            PsomlConfiguration.NeighbourhoodCoefficient = decimal.ToSingle(uxNeighbourhoodCoefficient.Value);
            PsomlConfiguration.LocalCoefficient = decimal.ToSingle(uxLocalCoefficient.Value);
            DialogResult = DialogResult.OK;
        }
    }
}